package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取学生成绩通知信息
func GetGradeNoticeInfo(p map[string]interface{}) *models.GradeNotice {
	o := orm.NewOrm()
	gradeNotice := &models.GradeNotice{}
	qs := o.QueryTable("GradeNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	qs.One(gradeNotice)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if gradeNotice.Id > 0 {
			gradeNotice.TotalGrade = gradeNotice.ChineseGrade + gradeNotice.MathematicsGrade +
				gradeNotice.EnglishGrade + gradeNotice.ChemistryGrade + gradeNotice.PhysicsGrade +
				gradeNotice.GeographyGrade + gradeNotice.HistoryGrade + gradeNotice.BiologyGrade +
				gradeNotice.PoliticsGrade
			gradeNotice.CreatedShow = gradeNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	return gradeNotice
}

// 添加学生成绩通知信息
func AddGradeNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	gradeNotice := &models.GradeNotice{}
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	gradeNotice.User = user
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	gradeNotice.Student = student
	if _, ok := input["test_name"]; ok {
		gradeNotice.TestName = input["test_name"][0]
	}
	if _, ok := input["chinese_grade"]; ok {
		gradeNotice.ChineseGrade = utils.Atof64(input["chinese_grade"][0])
	}
	if _, ok := input["mathematics_grade"]; ok {
		gradeNotice.MathematicsGrade = utils.Atof64(input["mathematics_grade"][0])
	}
	if _, ok := input["english_grade"]; ok {
		gradeNotice.EnglishGrade = utils.Atof64(input["english_grade"][0])
	}
	if _, ok := input["chemistry_grade"]; ok {
		gradeNotice.ChemistryGrade = utils.Atof64(input["chemistry_grade"][0])
	}
	if _, ok := input["physics_grade"]; ok {
		gradeNotice.PhysicsGrade = utils.Atof64(input["physics_grade"][0])
	}
	if _, ok := input["geography_grade"]; ok {
		gradeNotice.GeographyGrade = utils.Atof64(input["geography_grade"][0])
	}
	if _, ok := input["history_grade"]; ok {
		gradeNotice.HistoryGrade = utils.Atof64(input["history_grade"][0])
	}
	if _, ok := input["biology_grade"]; ok {
		gradeNotice.BiologyGrade = utils.Atof64(input["biology_grade"][0])
	}
	if _, ok := input["politics_grade"]; ok {
		gradeNotice.PoliticsGrade = utils.Atof64(input["politics_grade"][0])
	}
	return o.Insert(gradeNotice)
}

// 修改学生成绩通知信息
func EditGradeNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("GradeNotice").Filter("id", input["id"][0]).Update(p)
}

// 获取学生成绩通知列表
func GetGradeNoticeList(p map[string]interface{}) (int64, []*models.GradeNotice) {
	var gradeNoticeList []*models.GradeNotice
	o := orm.NewOrm()
	qs := o.QueryTable("GradeNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	if _, ok := p["schoolId"]; ok {
		qs = qs.Filter("Student__School__Id", p["schoolId"])
	}
	if _, ok := p["studentName"]; ok {
		qs = qs.Filter("Student__RealName", p["studentName"])
	}
	if _, ok := p["testName"]; ok {
		qs = qs.Filter("test_name", p["testName"])
	}
	if _, ok := p["classIds"]; ok && len(p["classIds"].([]int64)) > 0 {
		qs = qs.Filter("Student__Class__Id__in", p["classIds"])
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&gradeNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, gradeNotice := range gradeNoticeList {
			gradeNotice.TotalGrade = gradeNotice.ChineseGrade + gradeNotice.MathematicsGrade +
				gradeNotice.EnglishGrade + gradeNotice.ChemistryGrade + gradeNotice.PhysicsGrade +
				gradeNotice.GeographyGrade + gradeNotice.HistoryGrade + gradeNotice.BiologyGrade +
				gradeNotice.PoliticsGrade
			gradeNotice.CreatedShow = gradeNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, gradeNotice := range gradeNoticeList {
			gradeNotice.StudentName = gradeNotice.Student.RealName
			gradeNotice.CreatedDateShow = gradeNotice.Created.Format("2006-1-2")
			gradeNotice.TotalGrade = gradeNotice.ChineseGrade + gradeNotice.MathematicsGrade +
				gradeNotice.EnglishGrade + gradeNotice.ChemistryGrade + gradeNotice.PhysicsGrade +
				gradeNotice.GeographyGrade + gradeNotice.HistoryGrade + gradeNotice.BiologyGrade +
				gradeNotice.PoliticsGrade
		}
	}
	return totalCount, gradeNoticeList
}
